package org.me.marble;


import android.graphics.*;

import javax.microedition.khronos.opengles.GL10;

/**
 *
 * @author tungi
 */

public class Pictures{
    private final float EPSILON=0.000001f;
    
    public Texture background,life,pause;
    
    private int maxPaddingLeft,maxPaddingTop;
    private int display_width,display_height;
    private int maxTextureWidth=-1,maxTextureHeight=-1,ratio=1,
    			currentTextureWidth=-1,currentTextureHeight=-1;

    public Pictures(int display_width,int display_height){
        this.display_width=display_width;
        this.display_height=display_height;
    }

    public void loadTextures(boolean fog){        
        life=TextureFactory.loadTexture(R.drawable.lifeobjecticon,GL10.GL_TEXTURE_2D);
        pause=TextureFactory.loadTexture(R.drawable.pause,GL10.GL_TEXTURE_2D);                     
        
        if(fog)
        	return;
          
        //search a 2 power number, wich is the closest to the 
        //exact width and height of the screen -> only medium and high
        //density supported, so the wanted number is one of 256, 512 and 1024 
        //more: added texture quality support
        for(int i=8;i<12;++i){
        	if(display_width-Math.pow(2,i)<0 && maxTextureWidth==-1){
        		maxTextureWidth=(int)Math.pow(2,i);
        		if(OptionsLibrary.texture_quality.equals("low")){
        			ratio=4;
        			currentTextureWidth=(int)Math.pow(2,i-2);
        		}
        		else if(OptionsLibrary.texture_quality.equals("medium")){
        			ratio=2;
        			currentTextureWidth=(int)Math.pow(2,i-1);
        		}
        		else if(OptionsLibrary.texture_quality.equals("high")){
        			ratio=1;
    				currentTextureWidth=(int)Math.pow(2,i);
        		}
        	}
        	if(display_height-Math.pow(2,i)<0 && maxTextureHeight==-1){
        		maxTextureHeight=(int)Math.pow(2,i);
        		if(OptionsLibrary.texture_quality.equals("low")){
        			ratio=4;
        			currentTextureHeight=(int)Math.pow(2,i-2);
        		}
        		else if(OptionsLibrary.texture_quality.equals("medium")){
        			ratio=2;
        			currentTextureHeight=(int)Math.pow(2,i-1);
        		}
        		else if(OptionsLibrary.texture_quality.equals("high")){
        			ratio=1;        		
    				currentTextureHeight=(int)Math.pow(2,i);
        		}
        	}
        }        
        //load random bitmap
        int pic=(new java.util.Random()).nextInt(5)+1;
        int resId=GameActivity.game.getResources().
        			getIdentifier("background_game"+Integer.toString(pic),
        			"drawable",GameActivity.game.getPackageName());
        Bitmap bmp=BitmapFactory.decodeResource(GameActivity.game.getResources(),resId);
        //generate random paddings to BackgroundImage class
        int paddingLeft=(int)((bmp.getWidth()-maxTextureWidth)*Math.random());
        int paddingTop=(int)((bmp.getHeight()-maxTextureHeight)*Math.random());

        maxPaddingLeft=(display_width-maxTextureWidth)/2/ratio;
        maxPaddingTop=(display_height-maxTextureHeight)/2/ratio;
        
        background=TextureFactory.loadTexture(bmp,currentTextureWidth,
				currentTextureHeight,paddingLeft,paddingTop,GL10.GL_TEXTURE_2D);        
	}
    
    public void drawBackground(GL10 gl,Vector marble){
    	background.setWidth(maxTextureWidth);
        background.setHeight(maxTextureHeight);
        
    	double left=(Geometry.getCosAngle(new Vector(0,0,-1),
    			new Vector(marble.getX(),0,marble.getZ()))+1)/2;
    	if(marble.getX()>0) left=2-left;
    	double top=(Geometry.getCosAngle(new Vector(0,0,-1),
    			new Vector(0,marble.getY(),-1))+1)/2;
    	if(marble.getY()<0) top=2-top;
    	
        DrawTexture.drawAsImage(gl,background,new Vector(
        		(float)maxPaddingLeft*left,(float)maxPaddingTop*top,1-EPSILON),
        		new float[]{0,0,display_width/ratio,display_height/ratio});
        
        background.setWidth(currentTextureWidth);
        background.setHeight(currentTextureHeight);
    }

    public void drawPause(GL10 gl){
        Vector pos=new Vector((display_width-pause.getWidth())/2,
                           	  (display_height-pause.getHeight())/2,0);
        float[] crop=new float[]{0,0,pause.getWidth(),pause.getHeight()};

        DrawTexture.drawAsImage(gl,pause,pos,crop);
    }

    public void drawLifes(GL10 gl,int strength){
        int _w=life.getWidth();
        int _h=life.getHeight();
        int row=OptionsLibrary.lifes/5+1;
        
        float s=((float)strength+1f)/((float)MoveMarble.MAX_STRENGTH+1f);
        float[] crop={0,0,_w,_h};
        
        for(int i=0;i<row;++i){
            for(int j=1;j<=5;++j){
            	if(i*5+j>OptionsLibrary.lifes)
            		break;
            	
            	Vector pos=new Vector(display_width-j*(_w*1.5f+5),
                        display_height-(i+1)*(_h+5),0);
            	
            	life.setWidth((int)(life.getWidth()*1.5f));
            	
                if(i*5+j==OptionsLibrary.lifes){
                	life.setHeight((int)(life.getHeight()*s));
                	DrawTexture.drawAsImage(gl,life,pos,crop);
                	life.setHeight((int)(life.getHeight()/s));
                }
                else
                	DrawTexture.drawAsImage(gl,life,pos,crop);

                life.setWidth((int)(life.getWidth()*2/3));
            }
        }
    }
}
